<?php

return function ($oRes, $db_fadm, $method, $menuId,
        $parentId, $menuName, $icon, $menuType, $orderNum, $isFrame, $visible, $status, $path,
        $component) {
    $aWhere = array();
    $aWhere['menu_id'] = $menuId;
    $oSql = $db_fadm->sql()->table('system_menu');
    switch ($method) {
        case('get'):
            $data = $oSql->where($aWhere)->find();
            $oRes->data('data', $this->toCamel($data));
            break;
        case('list'):
            $aWhere = array();
            $sWhere = '1';
            if (strlen($menuName) > 0) {
                $aWhere['menu_name'] = '%' . $menuName . '%';
                $sWhere .= ' AND menu_name LIKE :menu_name';
            }
            if (strlen($status) > 0) {
                $aWhere['status'] = $status;
                $sWhere .= ' AND status=:status';
            }
            $oSql->where($aWhere, $sWhere);
            $oSql->order('parent_id,order_num');
            $data = $oSql->select();
            foreach ($data as &$mRow) {
                $mRow['menu_id'] = intval($mRow['menu_id']);
                $mRow['parent_id'] = intval($mRow['parent_id']);
            }
            $oRes->data('data', $this->toCamel($data));
            break;
        case('create'):
        case('update'):
            $mData = array();
            $mData['parent_id'] = $parentId;
            $mData['menu_name'] = $menuName;
            $mData['icon'] = $icon;
            $mData['menu_type'] = $menuType;
            $mData['order_num'] = $orderNum;
            $mData['is_frame'] = $isFrame;
            $mData['visible'] = $visible;
            $mData['status'] = $status;
            $mData['path'] = $path;
            if (intval($parentId) === 0) {
                $mData['component'] = 'Layout';
            }
            if ($menuType === 'C') {
                $mData['component'] = $component;
            }
            if ($method === 'create') {
                $mData['create_time'] = 'CURRENT_TIMESTAMP()';
                $oSql->add($mData);
            }
            if ($method === 'update') {
                $mData['update_time'] = 'CURRENT_TIMESTAMP()';
                $oSql->where($aWhere)->save($mData);
            }
            $db_fadm->commit();
            break;
        case('delete'):
            $oSql->where($aWhere)->delete();
            $db_fadm->commit();
            break;
    }
};
